<template>
  <div class="summary-outline-wrap">
    <div class="summary-outline-btn">
      <div class="outline-btn-edit">
        <span v-show="!isEdit" class="mr20" @click="editHandler">编辑</span>
        <p class="mr20" v-show="isEdit">
          <span class="mr20" @click="saveHandler">保存</span>
          <span @click="cancelHandler">取消</span>
        </p>
      </div>
      <span class="export-btn" v-pdf="{ el: 'item-detail-p4', title: '项目车辆内部验收单' }">导出</span>
      <!-- <div class="print" v-print="{ el: `#gf`, popTitle: '定制信息' }">打印</div> -->
    </div>
    <div class="item-detail-p4">
      <div class="summary-outline-title">项目车辆内部验收单</div>
      <div id="gf">
        <div class="summary-outline-content">
          <div class="outline-info">
            <div class="outline-info-title">基本信息</div>
            <ul class="outline-info-list">
              <!-- <li>
                <span>合同编号：</span>
                <span>{{ detail.orderModel?.contractCode || "--" }}</span>
              </li> -->
              <li>
                <span>客户名称：</span>
                <!-- <span>{{ detail.orderModel?.companyName || "--" }}</span> -->
                <span>{{ detail.companyName || "--" }}</span>
              </li>
              <li>
                <span>项目名称：</span>
                <span>{{ detail.name || "--" }}</span>
              </li>
            </ul>
            <ul class="outline-info-list">
              <li>
                <span>设备名称：</span>
                <span>{{ agvInfo.name || "--" }}</span>
              </li>
              <li>
                <span>设备数量：</span>
                <span>{{ agvInfo.count || "--" }}</span>
              </li>
              <li>
                <span>出厂日期：</span>
                <span>{{ agvInfo.time ? dayJs(agvInfo.time).format("YYYY/MM/DD") : "--" }}</span>
              </li>
            </ul>
            <ul class="outline-info-list">
              <li>
                <span>验收依据：</span>
                <span>核对AGV车辆以下数据信息</span>
              </li>
              <li>
                <span>验收地点：</span>
                <span>靖江</span>
              </li>
            </ul>
          </div>
          <div class="summary-outline-table">
            <TableContent :is-edit="isEdit" title="验收确认明细" :table-options="itemList" :is-hidden="true">
              <template #typeId="scope">
                <span>{{ getDictItem("checkType", scope.scope.typeId) || "--" }}</span>
              </template>
              <template #name="scope">
                <span>{{ scope.scope.name || "--" }}</span>
              </template>
              <template #parameter="scope">
                <span v-show="!isEdit">{{
                  (scope.scope.typeId === "1815323693371359232"
                    ? resultName1[scope.scope.parameter]
                    : resultName2[scope.scope.parameter]) || "--"
                }}</span>
                <div v-show="isEdit">
                  <el-radio-group v-model="scope.scope.parameter" v-if="scope.scope.typeId === '1815323693371359232'">
                    <el-radio v-for="(item, index) in Object.keys(resultName1)" :key="index" :value="item" :label="item">
                      {{ resultName1[item] }}
                    </el-radio>
                  </el-radio-group>

                  <el-radio-group v-model="scope.scope.parameter" v-else>
                    <el-radio v-for="(item, index) in Object.keys(resultName2)" :key="index" :value="item" :label="item">{{
                      resultName2[item]
                    }}</el-radio>
                  </el-radio-group>
                </div>
              </template>
              <template #remark="scope">
                <span v-show="!isEdit">{{ scope.scope.remark || "--" }}</span>
                <el-input v-show="isEdit" type="textarea" v-model="scope.scope.remark" autosize placeholder="备注"></el-input>
              </template>

              <template #actionOptions="scope">
                <span class="task-table-del" @click="deleteAgv(1, scope.scope)">删除</span>
              </template>
            </TableContent>
          </div>
          <div class="summary-outline-table">
            <div class="outline-info-title">验收结果</div>
            <span v-show="!isEdit">{{ checkDetail.status || "--" }}</span>
            <el-radio-group v-show="isEdit" v-model="checkDetail.status">
              <el-radio v-for="(item, index) in getDict('checkResult')" :key="index" :value="item.id" :label="item.name">
              </el-radio>
            </el-radio-group>
          </div>
          <div class="summary-outline-table">
            <div class="outline-info">
              <div class="outline-info-title">备注</div>
              <span v-show="!isEdit">{{ checkDetail.remark || "--" }}</span>
              <div v-show="isEdit">
                <el-input
                  type="textarea"
                  :disabled="!isEdit"
                  :rows="4"
                  :placeholder="isEdit ? '请填写' : ''"
                  v-model="checkDetail.remark"
                ></el-input>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script setup lang="ts">
import { reactive, ref, onMounted, inject, computed } from "vue";
import TableContent from "../components/TableContent/index.vue";
import { TableProps } from "@/components/Table/interface";
import { ElMessage } from "element-plus";
import { useDict } from "@/hooks/useDict";
import mittBus from "@/utils/mittBus";
import { updateProjectCheckDetail, getProjectCheckDetailProject } from "@/api/modules/project"; // getProjectBomPage addProjectTaskSaveOutcomeInfo
import { useHandleData } from "@/hooks/useHandleData";
import dayJs from "dayjs";
// hook
const { getDictItem, getDictId, getDict } = useDict(["checkType", "checkResult"]);
// 变量
const props = defineProps({
  detail: {
    type: Object,
    default: () => {}
  },
  task: {
    type: Object,
    default: () => {}
  }
});
const projectId = inject("projectId");
const isEdit = ref(false);
let tableDefault: TableProps = reactive({
  columns: [
    {
      type: "index",
      title: "序号",
      key: "",
      width: 48
    },
    {
      title: "类别",
      key: "typeId"
    },
    {
      title: "内容",
      key: "name",
      width: "2fr"
    },
    {
      title: "验收结论",
      key: "parameter"
    },
    {
      title: "备注",
      key: "remark",
      width: "2fr"
    }
    // {
    //   title: "操作",
    //   key: "actionOptions",
    //   width: 80
    // }
  ]
});
let itemList = ref({
  data: [],
  columns: tableDefault.columns
});
const resultName1 = ref({
  qualified: "有",
  unqualified: "无",
  unsuitable: "不适用"
});
const resultName2 = ref({
  qualified: "合格",
  unqualified: "不合格",
  unsuitable: "不适用"
});

const checkDetail = ref({});

// computed
const agvInfo = computed(() => {
  return {
    name: props.detail?.productModels.map(item => item.name).join(","),
    count: props.detail?.productModels.length,
    time: props.detail?.productModels[0]?.productionTime
  };
});

// 方法
const init = () => {
  getProjectCheckDetailProjectFn();
};
// 获取
const getProjectCheckDetailProjectFn = async () => {
  const { data } = await getProjectCheckDetailProject({ projectId, checkId: getDictId("projectCheckId") });
  //处理选中
  checkDetail.value = data;
  checkDetail.value.status = getDict("checkResult").find(item => item.id === checkDetail.value.status)?.name;
  checkDetail.value &&
    checkDetail.value.checkItems.forEach(item => {
      item.parameter =
        item.typeId === "1815323693371359232"
          ? Object.keys(resultName1.value).find(itm => item[itm])
          : Object.keys(resultName2.value).find(itm => item[itm]);
    });
  itemList.value.data = checkDetail.value.checkItems || [];
};
const editHandler = () => {
  isEdit.value = true;
};
const cancelHandler = () => {
  init();
  isEdit.value = false;
};
const saveHandler = async () => {
  try {
    // 处理保存参数
    checkDetail.value.status = getDict("checkResult").find(item => item.name === checkDetail.value.status)?.id;
    const obj = checkDetail.value;
    const params = {
      id: obj.id,
      remark: obj.remark,
      gmtCreate: obj.gmtCreate,
      gmtModified: obj.gmtModified,
      createBy: obj.createBy,
      modifiedBy: obj.modifiedBy,
      projectId: obj.projectId,
      checkId: obj.checkId,
      code: obj.code,
      name: obj.name,
      status: obj.status || "",
      checkItems: itemList.value.data.map(item => {
        console.log(item.parameter);
        if (item.typeId === "1815323693371359232") {
          Object.keys(resultName1.value).forEach(itm => {
            item[itm] = item.parameter == itm;
          });
        } else {
          Object.keys(resultName2.value).forEach(itm => (item[itm] = item.parameter == itm));
        }
        console.log(item.qualified);
        return {
          id: item.id,
          remark: item.remark,
          gmtCreate: item.gmtCreate,
          gmtModified: item.gmtModified,
          createBy: item.createBy,
          modifiedBy: item.modifiedBy,
          projectId: item.projectId,
          projectCheckId: item.projectCheckId,
          typeId: item.typeId,
          code: item.code,
          name: item.name,
          qualified: item.qualified,
          unqualified: item.unqualified,
          suitable: item.suitable,
          unsuitable: item.unsuitable,
          userId: item.userId,
          userName: item.userName,
          checkItemImg: item.checkItemImg,
          checkId: item.checkId
        };
      })
    };
    console.log(itemList.value.data);
    await updateProjectCheckDetail(params);
    isEdit.value = false;
    refresh();
    ElMessage.success("保存成功");
  } catch (e) {
    console.log(e);
  }
};
const refresh = () => {
  getProjectCheckDetailProjectFn();
  mittBus.emit("ProjectFileTime");
};
const deleteAgv = async (id, row) => {
  try {
    await useHandleData(
      () =>
        new Promise(resolve => {
          itemList.value.data.splice(row.serialNumber, 1);
          resolve(true);
        }),
      {},
      "删除"
    );
  } catch (e) {
    console.log(e);
  }
};

onMounted(() => {
  getProjectCheckDetailProjectFn();
});
</script>
<style lang="scss" scoped>
.summary-outline-wrap {
  box-sizing: border-box;
  min-height: 100%;
  padding: 20px;
  background: #ffffff;
  p {
    margin: 0;
  }
  i {
    font-style: normal;
  }
  .mr20 {
    margin-right: 20px;
  }
  .mt10 {
    margin-top: 10px;
  }
  .summary-outline-btn {
    display: flex;
    justify-content: flex-end;
    font-size: 16px;
    font-weight: 400;
    color: var(--el-color-primary);
    .outline-btn-edit {
      display: flex;
      span {
        cursor: pointer;
      }
    }
  }
  .summary-outline-title {
    margin-top: 12px;
    margin-bottom: 60px;
    font-size: 26px;
    font-weight: 600;
    color: #333333;
    text-align: center;
  }
  .summary-outline-content {
    margin: 0 $project-padding;
  }
  .summary-outline-table {
    margin-top: 30px;
    overflow: hidden;
    &-content {
      ul,
      li {
        padding: 0;
        margin: 0;
        list-style: none;
      }
    }
    .table-content-list {
      display: grid;
      grid-template-columns: 1fr 1fr;
      li {
        margin-bottom: 20px;
        font-size: 14px;
        font-weight: 400;
        span:first-child {
          margin-right: 10px;
          color: #666666;
        }
        span:last-child {
          color: #333333;
        }
      }
    }
  }
  .summary-outline-other {
    margin-top: 30px;
  }
  .outline-info {
    ul,
    li {
      padding: 0;
      margin: 0;
      list-style: none;
    }
    &-title {
      margin-bottom: 20px;
      font-size: 16px;
      font-weight: 600;
      color: #333333;
    }
    &-list {
      display: grid;
      grid-template-columns: 1fr 1fr 1fr;
      column-gap: 10px;
      width: 100%;
      margin-bottom: 10px !important;
      li {
        display: flex;
        align-items: center;
        overflow: hidden;
        font-size: 14px;
        font-weight: 400;
        span:first-child {
          // width: 100px;
          margin-right: 10px;
          color: #666666;
        }
        span:last-child {
          flex: 1;
          overflow: hidden;
          color: #333333;
          text-overflow: ellipsis;
          white-space: nowrap;
        }
      }
      .info-width {
        width: 109px;
        text-align: right;
      }
    }
  }
  :deep(.el-input),
  :deep(.el-textarea) {
    .el-input__wrapper {
      cursor: default;
      background: $project-background;
      .el-input__inner {
        cursor: default;
      }
    }
    .el-textarea__inner {
      cursor: default;
      resize: none;
      background: $project-background;
      box-shadow: none;
    }
  }
  :deep(.custom-title-edit) {
    .el-input__wrapper {
      cursor: default;
      background-color: #ffffff;
      box-shadow: none;
      .el-input__inner {
        cursor: default;
      }
    }
    .el-textarea__inner {
      cursor: default;
      background-color: #ffffff;
      box-shadow: none;
    }
  }
  :deep(.el-select) {
    width: 100% !important;
  }
  :deep(.el-radio) {
    color: #333333;
    .el-radio__input.is-checked .el-radio__inner {
      color: #333333;
      background: transparent;
      border-color: #333333;
    }
    .el-radio__inner::after {
      background: #333333;
    }
    .el-radio__input.is-checked + .el-radio__label {
      color: #333333;
    }
  }
}
</style>
